package com.augurit.wf.app.document.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.wf.app.document.service.IdocCategory;
import com.augurit.wf.app.document.utils.DocTreeNode;
import com.augurit.wf.app.document.utils.Version;
import com.augurit.wf.app.document.web.form.DocCategoryForm;



@SuppressWarnings("serial")
public class CategoryAction extends ExtBaseAction {
	
	@Autowired
	private IdocCategory docCategoryImpl;
	
	private Long dirId;

	private Long id;       //目录id
	private String version;//目录版本
	private String  partyType;//
	private String text;
	private Long pid;
	
	public Long getPid() {
		return pid;
	}
	public void setPid(Long pid) {
		this.pid = pid;
	}
	
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getPartyType() {
		return partyType;
	}
	public void setPartyType(String partyType) {
		this.partyType = partyType;
	}
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	
	private DocCategoryForm form;

	public DocCategoryForm getForm() {
		return form;
	}
	public void setForm(DocCategoryForm form) {
		this.form = form;
	}


   public  String catupdate() throws Exception{
	   boolean a=docCategoryImpl.catUpdate(this.id, this.text);
	   if(a){
		   this.extRenderSuccess();
	   }else{
		   this.extRenderFailture();
	   }
	   return null;
	   
   }
   
   
   public String list() throws Exception {
		List<DocCategoryForm> list = docCategoryImpl.findAllCat();
		this.renderJson(list);
		return null;
	}
   
   //获取版本列表
   public String versionList() throws Exception{
	   List<Version> list = docCategoryImpl.getVersionList();
	   CategoryAction.renderJson(list);
	   return null;
	   
   }
   public String catdelete() throws Exception{
	   try {
		   System.out.println(this.dirId);
		docCategoryImpl.deleteDir(this.getSysRealPath(),this.dirId);
		this.extRenderSuccess();
	} catch (RuntimeException e) {
		// TODO Auto-generated catch block
		this.extRenderFailture();
		System.out.println(e);
	}
	   
	   
	   return null;
   }
   
   public String catsave() throws Exception{
	   if(this.version == null ){
			this.version = this.getSysVersion();
		}
	   DocCategoryForm form =null;
	  if(this.text!=null && this.pid !=null){
		  form =new DocCategoryForm();
		  form.setName(this.text);
		  form.setPid(this.pid);
		  form.setVersion(this.version);
		  
	  }
	   if(form !=null)
	   { boolean a=docCategoryImpl.catSave(form);
	      if(a){
		      this.extRenderSuccess();
	        }else{
	         this.extRenderFailture();
	        } 
	   }else{
		   this.extRenderFailture();
	   }
	   return null;
	   
   }



	public String viewtree()throws Exception{
		if(version == null ){
			version = this.getSysVersion();
		}
		if(partyType!="DOC"){
			
		if(this.id != null && this.version != null){
			List<DocTreeNode> tree = docCategoryImpl.buildTree(this.id, version);
			if(tree!=null && tree.size()>0){
				String json = getJsonString(tree);
				this.renderJson(json);
			}else{
				this.renderText("[]");
			}	
		}}else{
			this.renderText("[]");
		}
		return null;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getDirId() {
		return dirId;
	}
	public void setDirId(Long dirId) {
		this.dirId = dirId;
	}
	
	
	
	
	
	
	


}
